package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.ReservationOrderManagementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ReservationOrderManagemenService;
import com.itheima.untils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReservationOrderManagemenService.class)
@Transactional
public class ReservationOrderManagemenServiceImpl implements ReservationOrderManagemenService {
    @Autowired
    private ReservationOrderManagementDao reservationOrderManagementDao;

    /*查询预约管理内容*/
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Map<String, Object>> page = reservationOrderManagementDao.conditionFindSAll(queryString);
        long total = page.getTotal();
        List<Map<String, Object>> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public List<Setmeal> findAll() {
        return reservationOrderManagementDao.findAll();
    }

    /*新增预约信息*/
    @Override
    public void add(Map map, Integer[] setmealIds) throws Exception {
        Member member = new Member();
        String name = (String) map.get("name");
        String phoneNumber = (String) map.get("phoneNumber");
        String orderDate = (String) map.get("orderDate");
        member.setPhoneNumber(phoneNumber);
        member.setName(name);
        Order order = new Order();
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType("电话预约");
        order.setOrderStatus("未到诊");

        reservationOrderManagementDao.add(member);
        order.setMemberId(member.getId());
        Integer memberId = member.getId();

        if (setmealIds != null && setmealIds.length > 0){
            for (Integer setmealId : setmealIds) {
                order.setSetmealId(setmealId);
                reservationOrderManagementDao.setmemberAndsetsetmeal(order);
            }
        }
    }


    @Override
    public void deleteOne(int id) {
        reservationOrderManagementDao.deleteByGId(id);

    }


    //编辑预约信息
    public void edit(Integer[] setmealIds, Map<String, String> map) throws Exception {
        //取出所有会员表的信息
        String name = map.get("name");//姓名
        //取出所有订单表的信息
        Integer member_id = Integer.parseInt(map.get("member_id"));//会员id
        String orderType = map.get("orderType");//预约类型
        String orderDate = map.get("orderDate");//预约时间
        String orderStatus = map.get("orderStatus");//预约时间
        if (orderStatus.equals("2")&&orderStatus!=null){
            orderStatus = "未到诊";
        }
        if (orderStatus.equals("1")&&orderStatus!=null){
            orderStatus = "已到诊";
        }
        //设置会员值
        Member member = new Member();
        member.setId(member_id);
        member.setName(name);
        //修改会员信息
        reservationOrderManagementDao.editm(member);
        Map<String, Object> hsMap = new HashMap<>();
        hsMap.put("member_id", member_id);
        hsMap.put("orderType", orderType);
        hsMap.put("orderDate", orderDate);
        hsMap.put("orderStatus", orderStatus);
        if (setmealIds.length==1){
            reservationOrderManagementDao.edito(hsMap);
        }
        if(setmealIds != null && setmealIds.length>1){
            reservationOrderManagementDao.deleteByGId(Integer.parseInt(map.get("id")));
            for (Integer setmealId : setmealIds) {
                hsMap.put("setmeal_id", setmealId);
                reservationOrderManagementDao.addOrder(hsMap);
            }
        }

    }

    @Override
    public List<Setmeal> findById(Integer id) throws Exception {
        return reservationOrderManagementDao.findById(id);
    }

    //更改预约状态
    public void setOrderStatus(Integer id) {
        //通过ID获取订单的所有信息
        Order order = reservationOrderManagementDao.findOrderById(id);
        String orderStatus = order.getOrderStatus();
        if ("未到诊".equals(orderStatus)) {
            reservationOrderManagementDao.editStatus(id,"已到诊");
        }
    }



}
